package com.fs.jzzn.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fs.base.utils.ApplicationUtils;
import com.fs.base.vo.JsonResponse;
import com.fs.base.vo.SysPath;
import com.fs.jzzn.pojo.Project;
import com.fs.jzzn.pojo.TheLibrary;
import com.fs.jzzn.repository.ProjectRepository;
import com.fs.jzzn.service.TheLibraryService;
/**
 * 出库管理controller
 * @author ACE
 */
@Controller
@Log4j
@RequestMapping("/console/theLibrary/rewrite")
public class TheLibraryController {
	@Autowired
	private TheLibraryService theLibraryService;
	@Autowired
	private ProjectRepository projectRepository;
	
	@RequestMapping("/detailedList")
	public String detailedList(HttpServletRequest req){
		String tid = req.getParameter("tid");
		Project project = projectRepository.findOneBypNo(tid);
		req.setAttribute("tid",tid);
		req.setAttribute("tName",project.getPRoad());
		return SysPath.BACK_PATH.concat("/theLibrary").concat("/detailedList");
	}
	@RequestMapping("/add")
	public String add(HttpServletRequest req){
		String id = req.getParameter("id");
		req.setAttribute("id", id);
		return SysPath.BACK_PATH.concat("/theLibrary").concat("/detailedListadd");
	}
	
	@ResponseBody
	@RequestMapping("/getName")
	public String getName(HttpServletRequest req){
		String tid = req.getParameter("tid");
		Project project = projectRepository.findOneBypNo(tid);
		return StringUtils.isBlank(project.getPRoad()) ? "" : project.getPRoad();
	}
	
	/**
	 * 出库新增重写
	 * @param storage
	 * @return
	 */
	@ResponseBody
	@RequestMapping("newAdd")
	public JsonResponse newAdd(TheLibrary storage) {
		try {
			theLibraryService.saveOrUpdata(storage);
			return JsonResponse.success();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return JsonResponse.error(e.getMessage());
		}
	}
	
	/**
	 * 出库编辑重写
	 * @param storage
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("modify")
	public JsonResponse modify(TheLibrary storage, HttpServletRequest request) {
		try {
			Map<String, Object> map = ApplicationUtils.getRequestDatas();
			TheLibrary oldVo = theLibraryService.getTheLibraryRepository().findOne(storage.getId());
			
			String oldJson = JSON.toJSONString(oldVo);
			@SuppressWarnings("unchecked")
			Map<String, Object> oldMap = JSON.parseObject(oldJson, Map.class);
			oldMap.putAll(map);
			
			String json = JSON.toJSONString(oldMap) ;
			
			TheLibrary newVo = JSON.parseObject(json, TheLibrary.class);
			theLibraryService.saveOrUpdata(newVo);
			return JsonResponse.success();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return JsonResponse.error(e.getMessage());
		}
	}
	
	/*
	 * 出库确认
	 */
	@ResponseBody
	@RequestMapping("confirm")
	public JsonResponse confirm(String id, HttpServletRequest request) {
		try {
			return theLibraryService.confirm(id);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return JsonResponse.error(e.getMessage());
		}
	}
	
	/**
	 * 出库删除
	 * @param id
	 * @return
	 */
	@GetMapping("delete")
	@ResponseBody
	public JsonResponse delete(String id) {
		try {
			theLibraryService.delete(id);
			return JsonResponse.success();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return JsonResponse.error(e.getMessage());
		}
	}
	
}
